# -*- coding:utf-8 -*-
import unittest
import ddt,requests
from django_web_token.jwt_demo.cs_jiekou.gonggong import Opera,maoyan_requests

df = Opera('D:\pythonProject1\django_web_token\jwt_demo\cs_jiekou\maoyanz_cs.xlsx')
@ddt.ddt
class Test_yl(unittest.TestCase):

    @classmethod
    def setUpClass(cls) -> None:
        cls.result_list = []
        # 获取URL
        a_url = 'http://127.0.0.1:8000'

        get_url = a_url + '/commodity'
        proxies = {
            'http': 'http://127.0.0.1:8888'
        }
        # 创建一个会话
        cls.session = requests.session()
        # 获取登录的URL
        login_url = a_url + '/login'
        # 登录用户密码
        login_data = {
            'username': 'admin',
            'password': '123456'
        }
        # 请求登录
        res = cls.session.post(login_url, data=login_data)
        # 保持会话
        cls.session.headers['Authorization'] = 'JWT ' + res.json()['token']
        # res = session.get(get_url, proxies=proxies)
    # 获取文件参数
    @ddt.data(*df.get_case_data())
    @ddt.unpack
    def test_cs(self,url,method,keys,data,status_code):

        # 判断是否为空
        if data:
            print(data)
            data = eval(data)
        try:
            res = maoyan_requests(url,method,keys,data)
            print(res)
            self.assertEqual(res.status_code,status_code)
            result = 'yes'
        except:
            result = 'no'
            raise
        finally:
            self.result_list.append(result)


    @classmethod
    def tearDownClass(cls) -> None:
        df.save_data('是否通过', cls.result_list)


if __name__ == '__main__':
    unittest.main()

